package ssi.staaar;

import java.util.ArrayList; 

import ssi.peeno.model.User;
import ssi.staaar.control.StaaarControl;
import ssi.staar.R;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.ContextMenu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.AdapterView.AdapterContextMenuInfo;

public class SearchFriendsActivity extends Activity {
	private StaaarControl stControl;
	private ArrayAdapter<String> usersAdapter;
	
	private void addUser(String user) {
		Log.d("[SEARCHFRIENDS]", "Trying to add: " + user);
		if (!isAlreadyFriend(ManageFriendsActivity.accepted, user))
			stControl.addUser(user);
	}
	
	private User[] excludeAlreadyFriend(User[] searchUser,
			ArrayList<User> accepted) {
		ArrayList<User> userList = new ArrayList<User>();
		for (int i=0; i<searchUser.length; ++i) {
			boolean present = false;
			for (int j=0; j<accepted.size(); ++j)
				if (accepted.get(j).getUsername().equals(searchUser[i].getUsername()))
					present = true;
			if (!present)
				userList.add(searchUser[i]);
		}
		User[] ret = new User[userList.size()];
		for (int i=0; i<userList.size(); ++i)
			ret[i] = userList.get(i);
		return ret;
	}

	private void initialize() {
		stControl = Staaar.stControl;
	}

	private boolean isAlreadyFriend(ArrayList<User> accepted, String user) {
		for (int i=0; i<accepted.size(); ++i)
			if (accepted.get(i).getUsername() == user)
				return true;
		return false;
	}

	@Override
	public boolean onContextItemSelected(MenuItem item) {
		AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
		switch (item.getItemId()) {
			case R.id.addUser:
				System.out.println(info.position);
				addUser(usersAdapter.getItem(info.position));
				return true;
			default:
				return super.onContextItemSelected(item);
		}
	}

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.searchfriends);
		initialize();
	}
	
	@Override
	public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
		super.onCreateContextMenu(menu, v, menuInfo);
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.searchfriendslistmenu, menu);
	}

	public void searchUser(View view) {
		EditText text = (EditText)findViewById(R.id.searchFriendsEditText1);
		User users[] = excludeAlreadyFriend(stControl.searchUser(text.getText().toString()), ManageFriendsActivity.accepted);
		Log.d("[SEARCHFRIENDS]", "" + users.length);
		String usernames[] = new String[users.length];
		for (int i=0; i<users.length; i++)
			usernames[i]=users[i].getUsername();
		ListView listview = (ListView)findViewById(R.id.searchFriendsList);
		usersAdapter = new ArrayAdapter<String>(this, R.layout.friend_list_item, usernames);
		listview.setAdapter(usersAdapter);
		registerForContextMenu(listview);
	}
}
